package com.ejoysoft.wlanyz.business;

import com.ejoysoft.auth.MD5;
import com.ejoysoft.common.*;
import com.ejoysoft.common.exception.UserUnitIdException;
import com.ejoysoft.wlanyz.system.Unit;
import com.ejoysoft.wlanyz.system.User;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.Vector;
import java.util.HashMap;
import java.util.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;

public class Task{
	private Globa globa;
    private DbConnect db;


    public Task() {
    }

    public Task(Globa globa) {
        this.globa = globa;
        db = globa.db;
    }

    public Task(Globa globa, boolean b) {
        this.globa = globa;
        db = globa.db;
        if (b) globa.setDynamicProperty(this);
    }

    String strTableName = "task_info";
    String strTableName1 = "task_test_result";
    String strTableName2 = "rm_device";
    
//    通过ap名称查询出该ap的mac地址
    public String findMac(String strName){
    	String strSql = "";
    	String strMac = "";
    	String where = " where device_name = " + " '" + strName + "'";
    	try {
    		strSql ="select mac_addr from  " + strTableName2 + "  ".concat(where);
    		ResultSet rs = db.executeQuery(strSql);
    		if (rs != null && rs.next()){
    			strMac = rs.getString("mac_addr");
                return strMac;
    		}
            else
                return null;
		} catch (Exception e) {
			return null;
		}
    }
	
	public boolean add() {
        String strSql = "";
        strId = UID.getID();
        try {
        	//把用户信息写入数据库
            strSql = "INSERT INTO " + strTableName + "  (strId, strName, strMac, strTestContent, strDesc,  dtCreatTime ,strCreator ) VALUES (?,?,?,?,?,to_date(?,'yyyy-MM-dd hh24:mi:ss'),?)";
            db.prepareStatement(strSql);
            db.setString(1, strId);
            db.setString(2, strName);
            db.setString(3, findMac(strName));   //strPWD
            db.setString(4, strTestContent);
            db.setString(5, strDesc);
            db.setString(6, Format.getDateTime());
            db.setString(7, globa.loginName);
   
            if (db.executeUpdate() > 0) {
                Globa.logger0("增加测试任务信息", globa.loginName, globa.loginIp, strSql, "任务管理", globa.userSession.getStrDepart());
                System.out.println(findMac(strName)+"------"+strName);
                return true;
            } else
                return false;
        } catch (Exception e) {
            System.out.println("增加用户信息时出错！！");
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean delete(String where) {
        try {
            String sql = "DELETE FROM " + strTableName + "  ".concat(where);
            db.executeUpdate(sql);
            Globa.logger0("删除任务信息", globa.loginName, globa.loginIp, sql, "任务管理", globa.unitCode);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
//返回单条记录
    public Task show(String where) {
        try {
            String strSql = "select * from  " + strTableName + "  ".concat(where);
            ResultSet rs = db.executeQuery(strSql);
            if (rs != null && rs.next())
                return load(rs, true);
            else
                return null;
        } catch (Exception e) {
            return null;
        }
    }
    public Task show1(String where) {
        try {
            String strSql = "select * from  " + strTableName1 + "  ".concat(where);
            ResultSet rs = db.executeQuery(strSql);
            if (rs != null && rs.next())
                return load1(rs, true);
            else
                return null;
        } catch (Exception e) {
            return null;
        }
    }

    /*public boolean update(String strId) {
        try {
            String strSql = "UPDATE  " + strTableName + "  SET  checkedLocation = ?, distance = ?, strCreator = ?, dtCreateTime = to_date(?,'yyyy-MM-dd hh24:mi:ss') WHERE strId=? ";
            db.prepareStatement(strSql);
            db.setString(1, checkedLocation);
            db.setString(2, String.valueOf(distance));
            db.setString(3, globa.loginName);
            db.setString(4, Format.getDateTime()); 
            db.setString(5, strId);
            db.executeUpdate();
             System.out.println("修改成功！！！");
            Globa.logger0("修改热点信息", globa.loginName, globa.loginIp, strSql, "热点信息", globa.userSession.getStrDepart());
            return true;
        } catch (Exception e) {
            System.out.println("修改用户信息时出错：" + e);
            return false;
        }
    }*/

    //记录集转化为对象
    public Task load (ResultSet rs,boolean isView){
		Task smLoad = new Task();
		try {
			smLoad.setStrId(rs.getString("strId"));
			smLoad.setStrName(rs.getString("strName"));
			smLoad.setStrMac(rs.getString("strMac"));
			smLoad.setStrTestContent(rs.getString("strTestContent"));
			smLoad.setStrDesc(rs.getString("strDesc"));
			smLoad.setDtCreatTime(rs.getString("dtCreatTime"));
			smLoad.setIntState(rs.getInt("intState"));
			smLoad.setStrCreator(rs.getString("strCreator"));
		} catch (Exception e){
			e.printStackTrace();
		}
		return smLoad;
	}
    
    public Task load1 (ResultSet rs,boolean isView){
		Task smLoad1 = new Task();
		try {
			smLoad1.setStrId(rs.getString("strId"));
			smLoad1.setStrTaskId(rs.getString("strTaskId"));
			smLoad1.setStrMac(rs.getString("strMac"));
			smLoad1.setRelevanceAPAvgTime(rs.getString("relevanceAPAvgTime"));
			smLoad1.setRelevanceAPSuccessRate(rs.getString("relevanceAPSuccessRate"));
			smLoad1.setSameFreInterference(rs.getString("sameFreInterference"));
			smLoad1.setNeiFreInterference(rs.getString("neiFreInterference"));
			smLoad1.setDtTestTime(rs.getString("dtTestTime"));
			smLoad1.setStrCreator(rs.getString("strCreator"));
			smLoad1.setDtCreatTime(rs.getString("dtCreatTime"));
			smLoad1.setStrPhone(rs.getString("strPhone"));
			
			
		} catch (Exception e){
			e.printStackTrace();
		}
		return smLoad1;
	}

  //查询符合条件的记录总数
    public int getCount(String where) {
        int count = 0;
        try {
            String sql = "SELECT count(strId) FROM " + strTableName + "  ";
            if (where.length() > 0) {
                where = where.toLowerCase();
                if (where.indexOf("order") > 0)
                    where = where.substring(0, where.lastIndexOf("order"));
                sql = String.valueOf(sql) + String.valueOf(where);
            }
            ResultSet rs = db.executeQuery(sql);
            if (rs.next())
                count = rs.getInt(1);
            rs.close();
            return count;
        } catch (Exception ee) {
            ee.printStackTrace();
            System.out.println(ee);           
            return count;
        }
    }

    public int getCountLast(String where) {
        int count = 0;
        try {
            String sql = "select count(strId) from task_info t where t.dtcreattime=(select max(dtcreattime) from task_info where strmac=t.strmac )";
            if (where.length() > 0) {
                where = where.toLowerCase();
                if (where.indexOf("order") > 0)
                    where = where.substring(0, where.lastIndexOf("order"));
                sql = String.valueOf(sql) + String.valueOf(where);
            }
            ResultSet rs = db.executeQuery(sql);
            if (rs.next())
                count = rs.getInt(1);   
            rs.close();
            return count;
           
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);           
            return count;
        }
    }


  //列表记录集
      public Vector<Task> list(String where, int startRow, int rowCount) {
        Vector<Task> beans = new Vector<Task>();
        try {
            String sql = "SELECT *  FROM  " + strTableName + " ";
//        	String sql = "select * from task_info t where t.dtcreattime=(select max(dtcreattime) from task_info where strmac=t.strmac )";
            if (where.length() > 0)
                sql = String.valueOf(sql) + String.valueOf(where);
            Statement s = db.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            if (startRow != 0 && rowCount != 0)
                s.setMaxRows((startRow + rowCount) - 1);
            ResultSet rs = s.executeQuery(sql);
            if (rs != null && rs.next()) {
                if (startRow != 0 && rowCount != 0)
                    rs.absolute(startRow);
                do {
                    Task theBean = new Task();
                    theBean = load(rs, false);
                    beans.addElement(theBean);
                } while (rs.next());
            }
            rs.close();
            s.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        return beans;
    }
      //列表记录集
      public Vector<Task> listLast(String where, int startRow, int rowCount) {
        Vector<Task> beans = new Vector<Task>();
        try {
        	String sql = "select * from task_info t where t.dtcreattime=(select max(dtcreattime) from task_info where strmac=t.strmac )";
            if (where.length() > 0)
                sql = String.valueOf(sql) + String.valueOf(where);
            Statement s = db.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            if (startRow != 0 && rowCount != 0)
                s.setMaxRows((startRow + rowCount) - 1);
            ResultSet rs = s.executeQuery(sql);
            if (rs != null && rs.next()) {
                if (startRow != 0 && rowCount != 0)
                    rs.absolute(startRow);
                do {
                    Task theBean = new Task();
                    theBean = load(rs, false);
                    beans.addElement(theBean);
                } while (rs.next());
            }
            rs.close();
            s.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        return beans;
    }
      //拿到状态为0的最近时间的任务集
      public Vector<Task> listLastState(String where, int startRow, int rowCount) {
          Vector<Task> beans = new Vector<Task>();
          try {
          	String sql = "select * from task_info t where t.dtcreattime=(select max(dtcreattime) from task_info where strmac=t.strmac) and t.intState =0";
              if (where.length() > 0)
                  sql = String.valueOf(sql) + String.valueOf(where);
              Statement s = db.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
              if (startRow != 0 && rowCount != 0)
                  s.setMaxRows((startRow + rowCount) - 1);
              ResultSet rs = s.executeQuery(sql);
              if (rs != null && rs.next()) {
                  if (startRow != 0 && rowCount != 0)
                      rs.absolute(startRow);
                  do {
                      Task theBean = new Task();
                      theBean = load(rs, false);
                      beans.addElement(theBean);
                  } while (rs.next());
              }
              rs.close();
              s.close();
          } catch (Exception ee) {
              ee.printStackTrace();
          }
          return beans;
      }

    private String strId;//	id号
    private String strName;
    private String strMac;
    private String strTestContent;
    private String strDesc;
    private String strCreator;
    private String dtCreatTime;
    
    
    private String strTaskId;
    private String relevanceAPAvgTime;
    private String relevanceAPSuccessRate;
    private String sameFreInterference;
    private String neiFreInterference;
    private String dtTestTime;
    private String strPhone;
    private int intState;//测试状态
   // private int i;
    
    
    
    
    public int getIntState() {
		return intState;
	}

	public void setIntState(int intState) {
		this.intState = intState;
	}

	public String getStrPhone() {
		return strPhone;
	}

	public void setStrPhone(String strPhone) {
		this.strPhone = strPhone;
	}

	public String getStrId() {
        return strId;
    }

    public void setStrId(String strId) {
        this.strId = strId;
    }

    public String getStrName() {
        return strName;
    }

    public void setStrName(String strName) {
        this.strName = strName;
    }
    
    public String getStrMac() {
		return strMac;
	}
    
    public void setStrMac(String strMac){
    	 this.strMac = strMac;
    }
    
    public String getStrTestContent() {
		return strTestContent;
	}
    
    public void setStrTestContent(String strTestContent){
    	this.strTestContent = strTestContent;
    }
    
    public String getStrDesc() {
		return strDesc;
	}
    
    public void setStrDesc(String strDesc) {
		this.strDesc = strDesc;
	}

    public String getStrCreator() {
		return strCreator;
	}
    
    public void setStrCreator(String strCreator) {
		this.strCreator = strCreator;
	}
 
   
    public String getDtCreatTime() {
        return dtCreatTime;
    }

    public void setDtCreatTime(String dtCreatTime) {
        this.dtCreatTime = dtCreatTime;
    }

    public String getStrTaskId() {
        return strTaskId;
    }

    public void setStrTaskId(String strTaskId) {
        this.strTaskId = strTaskId;
    }
    
   
    
   
    
    public String getRelevanceAPAvgTime() {
		return relevanceAPAvgTime;
	}
    
    public void setRelevanceAPAvgTime(String relevanceAPAvgTime) {
		this.relevanceAPAvgTime = relevanceAPAvgTime;
	}

    public String getRelevanceAPSuccessRate() {
		return relevanceAPSuccessRate;
	}
    
    public void setRelevanceAPSuccessRate(String relevanceAPSuccessRate) {
		this.relevanceAPSuccessRate = relevanceAPSuccessRate;
	}
 
    public String getSameFreInterference() {
		return sameFreInterference;
	}
    
    public void setSameFreInterference(String sameFreInterference) {
		this.sameFreInterference = sameFreInterference;
	}
    
    public String getNeiFreInterference() {
		return neiFreInterference;
	}
    
    public void setNeiFreInterference(String neiFreInterference) {
		this.neiFreInterference = neiFreInterference;
	}
    
    public String getDtTestTime() {
		return dtTestTime;
	}
    
    public void setDtTestTime(String dtTestTime) {
		this.dtTestTime = dtTestTime;
	}
    
	
}
